package com.richapp.launcher.ui.fragments.home.control;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.richapp.launcher.R;
import com.richapp.launcher.ui.fragments.BaseChildFragment;
import com.richapp.launcher.ui.fragments.BaseFragment;
import com.richapp.launcher.ui.view.TabPageView;

public class SmartControlFragment extends BaseFragment {
	private TabPageView mTabPageView;
	private ImageView light_control, curtains_control, power_monitor,
			camera_control, power_control, scenario_control;
    private View smartControl;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (smartControl == null) {
			smartControl = inflater.inflate(R.layout.smart_control, container, false);
			init(smartControl);
		}
		return smartControl;
	}

	protected void init(View view) {
		light_control = (ImageView) view.findViewById(R.id.light_control);
		curtains_control = (ImageView) view.findViewById(R.id.curtains_control);
		power_monitor = (ImageView) view
				.findViewById(R.id.power_monitor);
		camera_control = (ImageView) view.findViewById(R.id.camera_control);

		power_control = (ImageView) view.findViewById(R.id.power_control);
		scenario_control = (ImageView) view.findViewById(R.id.scenario_control);

		light_control.setOnKeyListener(onKeyListener);
		curtains_control.setOnKeyListener(onKeyListener);
		power_monitor.setOnKeyListener(onKeyListener);
		camera_control.setOnKeyListener(onKeyListener);
		
		light_control.setOnClickListener(click);
		curtains_control.setOnClickListener(click);
		power_monitor.setOnClickListener(click);
		camera_control.setOnClickListener(click);
		power_control.setOnClickListener(click);
		scenario_control.setOnClickListener(click);
	}
    private int isWhatBackListener;
	OnBackStackChangedListener backListener = new OnBackStackChangedListener (){

		@Override
		public void onBackStackChanged() {
			// TODO Auto-generated method stub
			switch (isWhatBackListener) {
			case LIGHTCONTROLFLAG:
				light_control.requestFocus();
				break;
			case CURTAINSCONTROLFLAG:
				curtains_control.requestFocus();
				break;
			case POWERMONITORFLAG:
				power_monitor.requestFocus();
				break;
			case CAMERACONTROLFLAG:
				camera_control.requestFocus();
				break;
			case POWERCONTROLFLAG:
				power_control.requestFocus();
				break;
			case SCENARIOCONTROLFLAG:
				scenario_control.requestFocus();
				break;

			default:
				break;
			}
		}
	};
	
	OnClickListener click = new OnClickListener() {

		@Override
		public void onClick(View v) {
			BaseChildFragment baseChildFragment = new BaseChildFragment();
			switch (v.getId()) {
			case R.id.light_control:
				isWhatBackListener = LIGHTCONTROLFLAG;
				baseChildFragment.setChildTag(isWhatBackListener);
				replaceFragment(baseChildFragment,R.id.tabs_fragment,backListener);
				break;
			case R.id.curtains_control:
				isWhatBackListener = CURTAINSCONTROLFLAG;
				baseChildFragment.setChildTag(isWhatBackListener);
				replaceFragment(baseChildFragment,R.id.tabs_fragment,backListener);
				break;
			case R.id.power_monitor:
				isWhatBackListener = POWERMONITORFLAG;
				baseChildFragment.setChildTag(isWhatBackListener);
				replaceFragment(baseChildFragment,R.id.tabs_fragment,backListener);
				break;
			case R.id.camera_control:
				isWhatBackListener = CAMERACONTROLFLAG;
				baseChildFragment.setChildTag(isWhatBackListener);
				replaceFragment(baseChildFragment,R.id.tabs_fragment,backListener);
				break;
			case R.id.power_control:
				isWhatBackListener = POWERCONTROLFLAG;
				baseChildFragment.setChildTag(isWhatBackListener);
				replaceFragment(baseChildFragment,R.id.tabs_fragment,backListener);
				break;
			case R.id.scenario_control:
				isWhatBackListener = SCENARIOCONTROLFLAG;
				baseChildFragment.setChildTag(isWhatBackListener);
				replaceFragment(baseChildFragment,R.id.tabs_fragment,backListener);
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onDestroyView() {
		((ViewGroup)smartControl.getParent()).removeView(smartControl);
		super.onDestroyView();
	}

	@Override
	public void onAttach(Activity activity) {
		mTabPageView = (TabPageView) activity.findViewById(R.id.tabPageView);
		super.onAttach(activity);
	}

	OnKeyListener onKeyListener = new OnKeyListener() {

		@Override
		public boolean onKey(View v, int keyCode, KeyEvent event) {
			boolean clickView = (v == light_control || v == curtains_control
					|| v == power_monitor || v == camera_control);
			if (clickView && (event.getAction() == KeyEvent.ACTION_DOWN)
					&& (keyCode == KeyEvent.KEYCODE_DPAD_UP)) {
				if (mTabPageView != null
						&& mTabPageView.getCurrTabView() != null) {
					mTabPageView.getCurrTabView().requestFocus();
				}
			}
			return false;
		}
	};

	public static SmartControlFragment getInstance() {
		return new SmartControlFragment();
	}
}
